package com.zhmc.myapplication.utils;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CalendarContract;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import com.google.android.material.textfield.TextInputLayout;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.TimeZone;

public class Utils {
    public static Uri calanderEventURL = CalendarContract.Events.CONTENT_URI;
    public static Uri calanderRemiderURL = CalendarContract.Reminders.CONTENT_URI;
    public static Uri calanderURL = CalendarContract.Calendars.CONTENT_URI;
    public static SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
    public static SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public static void setNavigationBarStatusBarTranslucent(Activity activity) {
        activity.getWindow().getDecorView().setSystemUiVisibility(5890);
        activity.getWindow().setNavigationBarColor(0);
        activity.getWindow().setStatusBarColor(0);
    }

    public static boolean isNotEmpty(Collection collection) {
        return collection != null && !collection.isEmpty();
    }

    public static String getDateStr(Date date) {
        return format.format(date);
    }

    public static String getDateAndTimeStr(Date date) {
        return format2.format(date);
    }

    public static String getNowTimeStr() {
        return format.format(new Date());
    }

    public static String getNowDateAndTimeStr() {
        return format2.format(new Date());
    }

    public static String getTILStr(TextInputLayout textInputLayout) {
        return textInputLayout.getEditText().getText().toString().trim();
    }

    public static void writeToCalendar(Context context, String title, String desc, String date) {
        String calId = "";
        Cursor userCursor = context.getContentResolver().query(calanderURL, null, null, null, null);
        if (userCursor.getCount() > 0) {
            userCursor.moveToFirst();
            calId = userCursor.getString(userCursor.getColumnIndex("_id"));
        }
        ContentValues event = new ContentValues();
        event.put("eventTimezone", TimeZone.getDefault().getID());
        event.put("title", title);
        event.put("description", desc);
        event.put("calendar_id", calId);
        long start = 0;
        try {
            start = format2.parse(date).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        event.put("dtstart", Long.valueOf(start));
        event.put("dtend", Long.valueOf(300000 + start));
        event.put("hasAlarm", (Integer) 1);
        event.put("eventTimezone", TimeZone.getDefault().getID());
        long id = Long.parseLong(context.getContentResolver().insert(calanderEventURL, event).getLastPathSegment());
        ContentValues values = new ContentValues();
        values.put("event_id", Long.valueOf(id));
        values.put("minutes", (Integer) 0);
        values.put("method", (Integer) 1);
        context.getContentResolver().insert(calanderRemiderURL, values);
        Log.e("xwf", "writeCalendarEvent 插入事件成功!!!");
    }

    public static void hideSoftKeyboard(Activity activity) {
        View view = activity.getCurrentFocus();
        if (view != null) {
            ((InputMethodManager) activity.getSystemService("input_method")).hideSoftInputFromWindow(view.getWindowToken(), 2);
        }
    }
}
